#include<bits/stdc++.h>
#define INF 2147483647
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef const long long cll;
const int N = 500010;
ll n;
struct Edge {
    ll to, next;
    Edge() {}
} edge[N];
ll head[N], cnt = 0;
void add(cll &u, cll &v) {
    edge[++cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}
ll size[N], centroid[2] = {0, INF}, wi[N], allsum = 0;
void GetCentroid(cll &father, cll &step) {
    size[step] = wi[step];
    for(ll i = head[step]; i; i = edge[i].next) {
        if(edge[i].to != father) {
            GetCentroid(step, edge[i].to);
            size[step] += size[edge[i].to];
        }
    }
    if(size[step] * 2 >= allsum) {
        centroid[centroid[0] != 0] = step;
    }
}
ll sum = 0;
void GetDistance(cll &father, cll &step, cll &num) {
    sum += num * wi[step];
    for(ll i = head[step]; i; i = edge[i].next) {
        if(edge[i].to != father) {
            GetDistance(step, edge[i].to, num + 1);
        }
    }
}
int main() {
    ll w, u, v;
    scanf("%lld", &n);
    for(ll i = 1; i <= n; i++) {
        scanf("%lld%lld%lld", &w, &u, &v);
        wi[i] = w;
        if(u != 0) { add(i, u); add(u, i); }
        if(v != 0) { add(i, v); add(v, i); }
        allsum += w;
    }
    GetCentroid(0, 1);
    GetDistance(0, centroid[0], 0);
    ll ans = sum;
    if(centroid[1] != INF) {
        sum = 0;
        GetDistance(0, centroid[1], 0);
        ans = min(ans, sum);
    }
//    printf("%lld %lld\n", centroid[0], centroid[1]);
    printf("%lld\n", ans);
    return 0;
}